<template>
  <el-card>
    <h2>用户管理</h2>
    <el-button type="primary" @click="openAdd">新增用户</el-button>
    <el-table :data="users" style="width: 100%;margin-top:16px">
      <el-table-column prop="userId" label="ID" width="60" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="role" label="角色" />
      <el-table-column prop="createdAt" label="创建时间" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDelete(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="passwordHash" v-if="!form.userId">
          <el-input v-model="form.passwordHash" type="password" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin" />
            <el-option label="前台" value="receptionist" />
            <el-option label="经理" value="manager" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getUsers, addUser, updateUser, deleteUser } from '../api/user';
import { ElMessage, ElMessageBox } from 'element-plus';

const users = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const form = ref({ userId: null, username: '', passwordHash: '', role: '' });
const formRef = ref();

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  passwordHash: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }]
};

const loadUsers = async () => {
  users.value = await getUsers();
};

const openAdd = () => {
  dialogTitle.value = '新增用户';
  form.value = { userId: null, username: '', passwordHash: '', role: '' };
  dialogVisible.value = true;
};

const openEdit = (row) => {
  dialogTitle.value = '编辑用户';
  form.value = { ...row, passwordHash: '' };
  dialogVisible.value = true;
};

const onSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return;
    if (!form.value.userId) {
      await addUser(form.value);
      ElMessage.success('新增成功');
    } else {
      await updateUser(form.value);
      ElMessage.success('修改成功');
    }
    dialogVisible.value = false;
    loadUsers();
  });
};

const onDelete = (id) => {
  ElMessageBox.confirm('确定要删除该用户吗？', '提示', { type: 'warning' })
    .then(async () => {
      await deleteUser(id);
      ElMessage.success('删除成功');
      loadUsers();
    });
};

onMounted(loadUsers);
</script> 